﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Code From: http://pradeepmahdevu.blogspot.com/2009/04/running-animations-in-mvvm.html
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Windows;
using System.Windows.Media.Animation;

#endregion

namespace ScrumTable.UI.View.Util
{
    public class ViewModelChangeDispatcher : FrameworkElement
    {
        #region Data

        public static readonly DependencyProperty SourceProperty;
        public static readonly DependencyProperty OnSourceChangedStartStoryboardProperty;

        // The routed event
        public static RoutedEvent SourceChangedEvent;

        #endregion // Data

        #region Static Constructor and static methods


        static ViewModelChangeDispatcher()
        {
            SourceProperty = DependencyProperty.Register(
                "Source",
                typeof(String),
                typeof(ViewModelChangeDispatcher),
                new FrameworkPropertyMetadata(string.Empty, OnSourcePropertyChanged));



            OnSourceChangedStartStoryboardProperty = DependencyProperty.Register(
                "OnSourceChangedStartStoryboard",
                typeof(Storyboard),
                typeof(ViewModelChangeDispatcher));


            //Registering the events
            SourceChangedEvent = EventManager.RegisterRoutedEvent("SourceChanged",
                                                                  RoutingStrategy.Bubble,
                                                                  typeof(RoutedEventHandler),
                                                                  typeof(ViewModelChangeDispatcher));

        }

        private static void OnSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            ViewModelChangeDispatcher vmcd = d as ViewModelChangeDispatcher;
            if (vmcd == null) return;
            if(e.OldValue!=null && e.OldValue.Equals(e.Property.DefaultMetadata.DefaultValue)) return;
            if(e.NewValue!=null && e.NewValue.Equals(e.OldValue)) return;

            

            vmcd.RaiseEvent(new RoutedEventArgs(SourceChangedEvent, vmcd));
            if (vmcd.OnSourceChangedStartStoryboard != null)
            {
                vmcd.OnSourceChangedStartStoryboard.Begin();
            }



        }



        #endregion

        #region constructor and members
        public ViewModelChangeDispatcher()
        {
            Visibility = Visibility.Collapsed;
            Width = 0;
            Height = 0;


        }

        /*
         * Source is always string. 
         * It is the onus of the markupextension to convert it to a string.
         * 
         * */
        public String Source
        {
            get { return (String)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }



        public event RoutedEventHandler SourceChanged
        {
            add { AddHandler(SourceChangedEvent, value); }
            remove { RemoveHandler(SourceChangedEvent, value); }
        }

        public Storyboard OnSourceChangedStartStoryboard
        {
            get { return (Storyboard)GetValue(OnSourceChangedStartStoryboardProperty); }
            set { SetValue(OnSourceChangedStartStoryboardProperty, value); }
        }





        #endregion


    }
}